package org.wanho.rent.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.wanho.rent.entity.Company;
import org.wanho.rent.entity.News;
import org.wanho.rent.entity.SysUser;
import org.wanho.rent.factory.ObjectFactory;
import org.wanho.rent.service.NewsService;
import org.wanho.rent.util.CommonUtil;
import org.wanho.rent.util.CommonsMultipartFile;
import org.wanho.rent.util.PageInfo;
import org.wanho.rent.util.ResponseUtil;

/**
 * 新闻控制器
 * @author student
 *
 */
@WebServlet(urlPatterns = "*.news")
public class NewsController extends HttpServlet {
	
	// private NewsService newsServiceProxy = (NewsService) ObjectFactory.getObject("newsServiceProxy");

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println(request.getServletPath() + "###");
		String servletPath = request.getServletPath();
		if (servletPath.equals("/addNews.news")) {
			addNews(request, response);
			System.out.println("-========+/news");
		} else if (servletPath.equals("/selectAllNews.news")) {
			selectAllNewsByPage(request, response);
		} else if (servletPath.equals("/updateNews.news")) {
			updateNews(request, response);
		} else if (servletPath.equals("/deleteNews.news")) {
			deleteNews(request, response);
		} else if (servletPath.equals("/selectNewsById.news")) {
			selectNewsById(request, response);
		}

	}

	/**
	 * 根据id查询一个新闻
	 * @param request
	 * @param response
	 */
	private void selectNewsById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String newsId = request.getParameter("newsId");
		System.out.println(newsId + "新闻id");

		// 返回数据到浏览器
		ResponseUtil.doResponse("成功！", new News(1, "大新闻...", "兴业银行南京分行阿萨德好啊斯的金卡或冻结卡大口喝酒啊斯的斯的...", new Date(),
				CommonUtil.getContextPath() + "/" + "Chrysanthemum.jpg"), true, response);
	}

	/**
	 * 分页查询所有新闻
	 * @param request
	 * @param response
	 */
	private void selectAllNewsByPage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub

		String pageNo = request.getParameter("pageNo");
		System.out.println(pageNo + "#");
		int page = Integer.parseInt(pageNo);

		List list = new ArrayList();
		list.add(new News(1, "兴业银行南京分行阿萨...",
				"兴业银行xcvxcvx行阿萨德好啊斯的金卡或冻结卡大口喝酒啊斯的斯cvbcvbccvx南京分行阿萨德好啊斯的金卡或冻结卡大口喝酒啊斯的斯的xcvxcvxv行阿萨德好啊斯的金卡或冻结卡大口喝酒啊斯的斯cvbcvbcbnvnvbn...",
				new Date(), CommonUtil.getContextPath() + "/" + "d0ad8b53-d704-4a30-8fce-9b9dcb4b471312.jpg"));
		list.add(new News(2, "兴业银行阿萨...",
				"兴业银行南京分行阿萨德好啊斯的金卡或冻结卡大口喝酒啊斯的斯cvbcvbcvbcvb行阿萨德好啊斯的金卡或冻结卡大口喝酒啊斯的斯cvbcvbccvbcvbcv行阿萨德好啊斯的金卡或冻结卡大口喝酒啊斯的斯cvbcvbcbcvbcbcvbcvbcvb的...",
				new Date(), CommonUtil.getContextPath() + "/" + "d0ad8b53-d704-4a30-8fce-9b9dcb4b471312.jpg"));
		list.add(new News(3, "大新闻...",
				"兴业银行南京分行阿萨德好啊斯的金卡或冻结卡大口喝酒ccvbcvb行阿萨德好啊斯的金卡或冻结卡大口喝酒啊斯的斯cvbcvbccvbc行阿萨德好啊斯的金卡或冻结卡大口喝酒啊斯的斯cvbcvbcvb行阿萨德好啊斯的金卡或冻结卡大口喝酒啊斯的斯cvbcvbcmbnmbnmbnmbnm行阿萨德好啊斯的金卡或冻结卡大口喝酒啊斯的斯cvbcvbcbnmbbnmbvb啊斯的斯的...",
				new Date(), CommonUtil.getContextPath() + "/" + "d0ad8b53-d704-4a30-8fce-9b9dcb4b471312.jpg"));
		// 返回数据到浏览器

		int pagesize = 2;
		if (page == 2) {
			ResponseUtil.doResponse("成功！",
					new PageInfo<News>(3, page, 2, list.subList((page - 1) * pagesize, list.size())), true, response);

		} else {
			ResponseUtil.doResponse("成功！",
					new PageInfo<News>(3, page, 2, list.subList((page - 1) * pagesize, pagesize * page)), true,
					response);
		}

	}

	/**
	 * 删除新闻
	 * @param request
	 * @param response
	 */
	private void deleteNews(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String newsId = request.getParameter("newsId");
		System.out.println("删除新闻" + newsId);

		// 返回数据到浏览器
		ResponseUtil.doResponse("成功！", null, true, response);
	}

	/**
	 * 修改新闻
	 * @param request
	 * @param response
	 */
	private void updateNews(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		Map<String, Object> map = null;
		try {
			map = CommonUtil.getParamsFromRequest(request);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		Object newsName = map.get("newsName");
		Object content = map.get("content");
		Object newsId = map.get("newsId");
		System.out.println("newsId" + newsId + "#" + newsName + "#" + content);
		List<CommonsMultipartFile> object = (List<CommonsMultipartFile>) map.get("fileList");
		for (CommonsMultipartFile file : object) {
			System.out.println(file.getOriginalFilename() + "@");
		}

		// 返回数据到浏览器
		ResponseUtil.doResponse("成功！", null, true, response);
	}

	/**
	 * 添加新闻
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	private void addNews(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub

		Map<String, Object> map = null;
		try {
			map = CommonUtil.getParamsFromRequest(request);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		Object newsName = map.get("newsName");
		Object content = map.get("content");
		System.out.println(newsName + "#" + content);
		List<CommonsMultipartFile> object = (List<CommonsMultipartFile>) map.get("fileList");
		for (CommonsMultipartFile file : object) {
			System.out.println(file.getOriginalFilename() + "@");
			
			/*
			 * 将图片文件保存到服务器磁盘上
			 */
			String upload = CommonUtil.getUploadPath();
			String filename = file.getOriginalFilename(); // baidu.jpg
			String name = UUID.randomUUID().toString() + filename.substring(filename.lastIndexOf("."));
			File f = new File(upload, name);
			System.out.println(f);
			try {
				file.transferTo(f);
			} catch (Exception e) {
				ResponseUtil.doResponse("图书上传出现异常！", null, false, response);
			}
			
			/*
			 * 插入数据库中
			 */
			News news = new News();
			news.setNewTitle((String)newsName);
			news.setNewContent((String)content);
			news.setCreateTime(new Date());
			
			String imageUrl = CommonUtil.getContextPath() + File.separator + name;
			System.out.println(imageUrl);
			news.setImageUrl(imageUrl);
			
//			newsServiceProxy.add(news);
			
			
		}
		// 返回数据到浏览器
		ResponseUtil.doResponse("成功！", null, true, response);
	}

}
